package com.zr.plant.controller;

import com.zr.plant.model.PlantAddVo;
import com.zr.plant.model.PlantSelectVo;
import com.zr.plant.model.PlantUpdateStatusVo;
import com.zr.plant.model.PlantUpdateVo;
import com.zr.plant.service.PlantService;
import com.zr.util.ResultBuilderVo;
import com.zr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by lenovo on 2019/12/25.
 */
@RestController
@CrossOrigin
public class PlantController {
    @Autowired
    private PlantService plantService;

    //下拉框接口
    @GetMapping("legalPerson/ofCurrentUser")
    public ResultVo queryLegalPerson(){
        //通过session获取id
        Integer userid = 1;
        //根据id获取法人信息
       return plantService.queryLegalPersonById(userid);
    }
    //分页接口
    @PostMapping("legalPlant/query")
    public ResultVo queryPage(@RequestBody PlantSelectVo plantSelectVo){
        return plantService.queryPage(plantSelectVo);
    }

    //新增接口
    @PostMapping("legalPlant/add")
    public ResultVo add(@RequestBody @Valid PlantAddVo plantAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return plantService.add(plantAddVo);
    }

    //修改接口
    @PostMapping("legalPlant/update")
    public ResultVo update(@RequestBody @Valid PlantUpdateVo plantUpdateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return plantService.update(plantUpdateVo);
    }
    //修改状态接口

    @PostMapping("legalPlant/updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid PlantUpdateStatusVo plantUpdateStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return plantService.updateStatus(plantUpdateStatusVo);
    }
}
